﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Autenticazione.Tools;

namespace Autenticazione
{
    public partial class Atleta : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BusinessObject.Cintura belt = new BusinessObject.Cintura();

            if (Session["EMail"] == null)
                Response.Redirect("Autenticazione.aspx");

            if (!IsPostBack)
            {
                Tools.Tools.FillComboMesi(cboBirthMonth);
                Tools.Tools.FillComboYears(cboBirthYear);
                Tools.Tools.FillComboDaysInMonthSelected(cboBirthDay, int.Parse(cboBirthMonth.SelectedValue), int.Parse(cboBirthYear.SelectedValue));
                Tools.Tools.FillComboGender(cboSesso);
                Tools.Tools.FillCombo(cboBelt, belt.GetTbBelts(), "Id", "Belt");
                Tools.Tools.FillGrapplingLevel(ddlGrapplingLevel);
            }
        }

        protected void btnSalvaAtleta_Click(object sender, EventArgs e)
        {
            //string strScript = @"<script language='JavaScript'>NavigateBack()</script>";
            BusinessObject.Atleta atleta = new BusinessObject.Atleta();

            if (Session["EMail"] == null)
                Response.Redirect("Autenticazione.aspx");

            atleta.EMail = Session["EMail"].ToString();
            atleta.FullName = this.txtFullName.Text.Replace("'", "''");
            atleta.Gender = int.Parse(cboSesso.SelectedValue);
            atleta.BirthDayString = string.Format("{0}/{1}/{2}", cboBirthYear.SelectedValue, cboBirthMonth.SelectedValue, cboBirthDay.SelectedValue);
            atleta.Belt = int.Parse(cboBelt.SelectedValue);
            atleta.Weight = float.Parse(txtPeso.Text);
            atleta.GrapplingLevel = ddlGrapplingLevel.SelectedValue; 
            atleta.MatchFullContact = int.Parse(txtMatchFullContact.Text);
            atleta.MatchLowKick = int.Parse(txtMatchLowKick.Text);
            atleta.MatchK1Rules = int.Parse(txtMatchK1Rules.Text);

            if (Page.IsValid)
            {
                atleta.Salva();
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", "window.close(); window.opener.Aggiorna()", true);
            }
        }

        protected void Year_Changed(object sender, EventArgs e)
        {
            Tools.Tools.FillComboDaysInMonthSelected(cboBirthDay, int.Parse(cboBirthMonth.SelectedValue), int.Parse(cboBirthYear.SelectedValue));
        }

        protected void Month_Changed(object sender, EventArgs e)
        {
            Tools.Tools.FillComboDaysInMonthSelected(cboBirthDay, int.Parse(cboBirthMonth.SelectedValue), int.Parse(cboBirthYear.SelectedValue));
        }
    }
}